package com.sskj.fabi.ui.fragment;


import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.component.DaggerUserDataComponent;
import com.sskj.fabi.presenter.FabiFragmentPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppDirectCheckUtil;
import com.sskj.lib.util.AppOuterCheckUtil;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.FABI_FRAGMENT)
public class FabiFragment extends BaseFragment<FabiFragmentPresenter> {
    @BindView(R2.id.segmentTabLayout)
    SegmentTabLayout segmentTabLayout;
    @BindView(R2.id.ivPublish)
    ImageView ivPublish;
    @BindView(R2.id.ivRecord)
    ImageView ivRecord;
    @BindView(R2.id.viewPager)
    ViewPager viewPager;
    private String[] titles = {App.INSTANCE.getString(R.string.fabi_fabiFragment1_h), App.INSTANCE.getString(R.string.fabi_fabiFragment2_h)};
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.fabi_fragment_main;
    }

    @Override
    public FabiFragmentPresenter getPresenter() {
        return new FabiFragmentPresenter();
    }

    @Override
    protected void initView() {

        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });

        segmentTabLayout.setTabData(titles);
        segmentTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.FABI_FRAGMENT_BUY_SELL).withBoolean(Constans.IS_BUY, true).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.FABI_FRAGMENT_BUY_SELL).withBoolean(Constans.IS_BUY, false).navigation());
        viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragments));
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                segmentTabLayout.setCurrentTab(position);
            }
        });

        ClickUtil.click(ivPublish, () -> {
            if (!AppOuterCheckUtil.checkHighAuth(getActivity(), userData)) {  // 高级认证
                return;
            }
            if (!AppOuterCheckUtil.checkShop(getActivity(), userData)) {
                return;
            }
//            if (!AppDirectCheckUtil.checkLogin(getActivity(), userData)) {
//                return;
//            }
//
//            if (!AppDirectCheckUtil.checkPayType(getActivity(), userData)) {
//                return;
//            }
            ARouter.getInstance().build(RConfig.FABI_PUBLISH).navigation();
        });
        ClickUtil.click(ivRecord, () -> {
            if (!AppDirectCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.FABI_ORDER_RECORD).navigation();

        });
    }
}
